package com.caimiao.miao.library.utils;

import android.content.Context;
import android.util.Log;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.caimiao.miao.bean.LocationInfo;
import com.caimiao.miao.interfaces.ILocUtil;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import rxlocation.github.pitt.ClientOption;
import rxlocation.github.pitt.LocationRequester;
import rxlocation.github.pitt.Platform;
import rxlocation.github.pitt.RxLocation;
//百度定位工具
public class BaiduLocUtil extends BDAbstractLocationListener implements ILocUtil {
    private final String TYPE_GCJ02 = "GCJ02";//国测局坐标；
    private final String TYPE_DB0911 = "BD09ll";//百度经纬度坐标；
    private final String TYPE_DB09 = "BD09";//百度墨卡托坐标；
    private LocationClient mLocationClient;
    private LocationListener mLocationListener;
    private LocationInfo mLocInfo;

    public BaiduLocUtil(Context context) {
        mLocationClient = new LocationClient(context.getApplicationContext());
        mLocationClient.registerLocationListener(this);

        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setCoorType(TYPE_DB0911);
        option.setOpenGps(true);
        //设置是否在stop的时候杀死这个定位service进程，默认（建议）不杀死(true)
        option.setIgnoreKillProcess(false);
        mLocationClient.setLocOption(option);
    }

    public void start() {
        mLocationClient.start();
    }

    @Override
    public ILocUtil setOnLocationListener(LocationListener listener) {
        mLocationListener = listener;
        return this;
    }

    @Override
    public void onReceiveLocation(BDLocation bdLocation) {
        int status = bdLocation.getLocType();
        Log.e("BaiduLoc", "status = " + status);
        double latitude = bdLocation.getLatitude();    //获取纬度信息
        double longitude = bdLocation.getLongitude();    //获取经度信息
        LocationInfo info = new LocationInfo(latitude, longitude);
        info.setType(bdLocation.getCoorType());
        mLocInfo = info;
        if (mLocationListener != null) mLocationListener.call(info);
    }
}
